package com.sansecy.monitor.see;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.ViewGroup;

import com.sansecy.monitor.utils.Utils;

/**
 * @Description TODO
 * @Date 2019-08-04 01:22
 * @Created by caoyu
 */
public class SeeActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {

    private SeeView mSeeView;

    public SeeActivityLifecycleCallbacks() {
    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {
        handleActivity(activity);
        ActivityManger.addActivity(activity);
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {
    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {
        if (mSeeView == null) {
            mSeeView = SeeView.getInstance();
            int statusBarHeight = Utils.getStatusBarHeight(activity);
            int navigationBarHeight = Utils.getNavigationBarHeight(activity);
            ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                    , ViewGroup.LayoutParams.MATCH_PARENT);
            layoutParams.topMargin = statusBarHeight;
            layoutParams.bottomMargin = navigationBarHeight;
            mSeeView.setLayoutParams(layoutParams);
        }
        mSeeView.attachToWindow(activity);
    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {
        if (mSeeView != null) {
            mSeeView.detachFromWindow(activity);
        }
    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {
    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {

    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {
        ActivityManger.removeActivity(activity);
    }

    private static void handleActivity(Activity activity) {
        if (activity instanceof FragmentActivity) {
            ((FragmentActivity) activity).getSupportFragmentManager()
                    .registerFragmentLifecycleCallbacks(new FragmentManager.FragmentLifecycleCallbacks() {
                        @Override
                        public void onFragmentCreated(@NonNull FragmentManager fm, @NonNull Fragment f, Bundle savedInstanceState) {
                            ActivityManger.addFragment(f);
                        }

                        @Override
                        public void onFragmentDestroyed(@NonNull FragmentManager fm, @NonNull Fragment f) {
                            ActivityManger.removeFragment(f);
                        }
                    }, true);
        }

//        ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
//        final SeeView seeView = new SeeView(activity);
//        seeView.setLayoutParams(new ViewGroup.MarginLayoutParams(-2, -2));
//        decorView.addView(seeView);
//        seeView.post(new Runnable() {
//            @Override
//            public void run() {
//                seeView.layout(SeeView.location);
//            }
//        });
//        decorView.addView(new LogView(activity));
//        WindowManager windowManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
//        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
//                -2,-2, WindowManager.LayoutParams.TYPE_APPLICATION,
//                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
////                        |WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
//                , PixelFormat.TRANSPARENT);
//        SeeView seeView = new SeeView(activity);
//        seeView.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                int x = (int) event.getX();
//                int y = (int) event.getY();
//
//                int action = event.getAction();
//                switch (action) {
//                    case MotionEvent.ACTION_MOVE:
//                        layoutParams.x = x;
//                        layoutParams.y = y;
//                        windowManager.updateViewLayout(seeView, layoutParams);
//                        break;
//                }
//                return true;
//            }
//        });
//        windowManager.addView(seeView, layoutParams);
    }

    public SeeView getSeeView() {
        return mSeeView;
    }
}
